<?php
/*
 * @author Carlos Avila
 */
Load::models('videos','subcategorias');
 
class VideoController extends AppController 
{	        
    
    public function index($page=1) 
    {        
        $video = new Videos();
        $this->videos = $video->getAllVideos($page);
    }                       
     
    public function crear($youtube_id='') 
    {        
        if(Input::hasPost('url')){            
            $youtube_id = Youtube::getYoutubeID(Input::post('url'));
            $youtube_id = Filter::get($youtube_id, 'striptags', 'stripslashes', 'trim');
            $this->youtube_id = $youtube_id;            
        }        
		       
        if(Input::hasPost('videos')){

            $video = new Videos(Input::post('videos'));						
			            
            if($video->save()){
                Flash::valid('Operación exitosa');
                Input::delete();
                return;               
            }else{
                Flash::error('Falló Operación');
            }
        }        
    }
    
    public function ver($video_id) 
    {        
        $this->video = Load::model("Videos")->getVideo($video_id);
    }
    
    public function editar($video_id) 
    {                        
        $videos = new Videos();
		
        if(Input::hasPost('videos')){
 
            if($videos->actualizar(Input::post('videos'))){
            	
                Flash::valid('Video actualizado correctamente');            
                return Router::redirect();
                
            } else {
                Flash::error('El video no pudo ser actualizado');
            }
            
        }else 
		{
            $this->videos = $videos->getVideo((int)$video_id);
            $this->categoria = Load::model('subcategorias')->getSubcategoria($videos->subcategorias_id)->categorias_id;
            
        }
    }
    
    public function borrar($video_id) 
    {                        
        $video = Load::model('videos')->getVideo($video_id);             
        if($video->borrar()){
            Flash::success ('El video a sido borrado');
            return Router::redirect();
        }
    }
                    
}
        
        